﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DanhmucsanphamController
/// </summary>
public class DanhmucsanphamController:Interface
{
    private SqlConnection con;
	public DanhmucsanphamController()
	{
	    con = ConnectDB.Connect();
        ConnectDB.Open();
	}

    public int Insert(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Insert_Danhmucsanpham";
            cmd.CommandType = CommandType.StoredProcedure;
            Danhmucsanpham dmsp = (Danhmucsanpham)obj;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dmsp.Name;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = dmsp.Url;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = dmsp.Description;
            cmd.Parameters.Add("@tag", SqlDbType.NText).Value = dmsp.Tag;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = dmsp.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = dmsp.Status;
            cmd.Parameters.Add("@dateStart", SqlDbType.DateTime).Value = dmsp.DateStart;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Update(object obj)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Update_Danhmucsanpham";
            cmd.CommandType = CommandType.StoredProcedure;
            Danhmucsanpham dmsp = (Danhmucsanpham)obj;
            cmd.Parameters.Add("@danhmuc_id", SqlDbType.Int).Value = dmsp.DanhmucSanpham_id;
            cmd.Parameters.Add("@name", SqlDbType.NVarChar).Value = dmsp.Name;
            cmd.Parameters.Add("@url", SqlDbType.NText).Value = dmsp.Url;
            cmd.Parameters.Add("@description", SqlDbType.NText).Value = dmsp.Description;
            cmd.Parameters.Add("@tag", SqlDbType.NText).Value = dmsp.Tag;
            cmd.Parameters.Add("@order", SqlDbType.Int).Value = dmsp.Order;
            cmd.Parameters.Add("@status", SqlDbType.Bit).Value = dmsp.Status;
            cmd.Parameters.Add("@dateUpdate", SqlDbType.DateTime).Value = dmsp.DateUpdate;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public int Delete(int id)
    {
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandText = "Delete_Danhmucsanpham";
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@danhmuc_id", SqlDbType.Int).Value = id;
            return cmd.ExecuteNonQuery();
        }
        catch (Exception)
        {
            return 0;
            throw;
        }
    }

    public DataTable GetAll()
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Danhmucsanpham ORDER BY [order] ASC";
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {

            return null;
            throw;
        }
    }

    public DataTable GetByProductId(int productId)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            string select = "select Danhmucsanpham.name from Product_Danhmuc inner join Danhmucsanpham ";
                   select+="on Danhmucsanpham.danhmuc_id=Product_Danhmuc.danhmuc_id inner join Product on Product_Danhmuc.product_id ";
                   select+="=Product.product_id AND Product.product_id=@productId";
            cmd.CommandText = select;
            cmd.Parameters.AddWithValue("productId", productId);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {


            throw;
        }
    }

    public DataTable GetByID(int id)
    {
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "SELECT * FROM Select_All_Danhmucsanpham WHERE danhmuc_id=@danhmuc_id ORDER BY [order] ASC";
            cmd.Parameters.AddWithValue("@danhmuc_id", id);
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter();
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
        catch (Exception)
        {
            return null;
            throw;
        }
    }

}